package com.zmn.oms.business.impl.conf.attachment;

import com.alibaba.fastjson.JSON;
import com.beust.jcommander.internal.Lists;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.business.interfaces.conf.attachment.ConfOrderAttachmentStandardBService;
import com.zmn.oms.model.dto.conf.attachment.ConfOrderAttachmentStandardDTO;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentAuditStandard;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentStandard;
import com.zmn.oms.services.interfaces.conf.attachment.ConfOrderAttachmentStandardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述: 附件审核标准配置 逻辑处理类
 *
 * @author: liudi
 * @since: 2022/01/25 14:18
 */
@Service
@Slf4j
public class ConfOrderAttachmentStandardBServiceImpl implements ConfOrderAttachmentStandardBService {

	@Resource
	private ConfOrderAttachmentStandardService confOrderAttachmentStandardService;

	@Override
	public ConfOrderAttachmentStandardDTO listByQuery(ConfOrderAttachmentStandard query) {
		Objects.requireNonNull(query);

		List<ConfOrderAttachmentStandard> standardList = confOrderAttachmentStandardService.listByQuery(query);
		ConfOrderAttachmentStandardDTO dto = new ConfOrderAttachmentStandardDTO();

		if (CollectionUtil.isNullOrEmpty(standardList)) {
			dto.setAuditStandardList(Lists.newArrayList());
			return dto;
		}

		ConfOrderAttachmentStandard standard = standardList.get(0);
		BeanMapper.copy(standard, dto);
		dto.setAuditStandard("");

		// 将审核标准json字符串转成集合，将最后的结果放到集合中
		List<List<ConfOrderAttachmentAuditStandard>> auditStandardList = standardList.stream()
				.map(ConfOrderAttachmentStandard::getAuditStandard)
				.filter(StringUtil::isNotBlank)
				.map(item -> JSON.parseArray(item, ConfOrderAttachmentAuditStandard.class))
				.collect(Collectors.toList());

		if (CollectionUtil.isNullOrEmpty(auditStandardList)) {
			auditStandardList = Lists.newArrayList();
		}

		dto.setAuditStandardList(auditStandardList);
		return dto;
	}
}
